{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "da6a2c1b",
   "metadata": {},
   "source": [
    "# 1\n",
    "加载原始数据并显示表格(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2c5ee3e5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Warm-blooded</th>\n",
       "      <th>Gives Birth</th>\n",
       "      <th>Aquatic Creature</th>\n",
       "      <th>Aerial Creature</th>\n",
       "      <th>Has Legs</th>\n",
       "      <th>Hiberates</th>\n",
       "      <th>Class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>human</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>python</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>salmon</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>whale</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>frog</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>amphibians</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>komodo</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>bat</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>pigeon</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>cat</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>leopard shark</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>turtle</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>penguin</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>porcupine</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>eel</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>salamander</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>amphibians</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>koala</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>flamingo</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>beaver</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>giraffe</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>seagull</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>iguana</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>otter</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>albatross</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>kangaroo</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>trout</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Name  Warm-blooded  Gives Birth  Aquatic Creature  \\\n",
       "0           human             1            1                 0   \n",
       "1          python             0            0                 0   \n",
       "2          salmon             0            0                 1   \n",
       "3           whale             1            1                 1   \n",
       "4            frog             0            0                 1   \n",
       "5          komodo             0            0                 0   \n",
       "6             bat             1            1                 0   \n",
       "7          pigeon             1            0                 0   \n",
       "8             cat             1            1                 0   \n",
       "9   leopard shark             0            1                 1   \n",
       "10         turtle             0            0                 1   \n",
       "11        penguin             1            0                 1   \n",
       "12      porcupine             1            1                 0   \n",
       "13            eel             0            0                 1   \n",
       "14     salamander             0            0                 1   \n",
       "15          koala             1            1                 0   \n",
       "16       flamingo             1            0                 0   \n",
       "17         beaver             1            1                 0   \n",
       "18        giraffe             1            1                 0   \n",
       "19        seagull             1            0                 1   \n",
       "20         iguana             0            0                 0   \n",
       "21          otter             1            1                 0   \n",
       "22      albatross             1            0                 1   \n",
       "23       kangaroo             1            1                 0   \n",
       "24          trout             0            0                 1   \n",
       "\n",
       "    Aerial Creature  Has Legs  Hiberates       Class  \n",
       "0                 0         1          0     mammals  \n",
       "1                 0         0          1    reptiles  \n",
       "2                 0         0          0      fishes  \n",
       "3                 0         0          0     mammals  \n",
       "4                 0         1          1  amphibians  \n",
       "5                 0         1          0    reptiles  \n",
       "6                 1         1          1     mammals  \n",
       "7                 1         1          0       birds  \n",
       "8                 0         1          0     mammals  \n",
       "9                 0         0          0      fishes  \n",
       "10                0         1          0    reptiles  \n",
       "11                0         1          0       birds  \n",
       "12                0         1          1     mammals  \n",
       "13                0         0          0      fishes  \n",
       "14                0         1          1  amphibians  \n",
       "15                0         1          0     mammals  \n",
       "16                1         1          0       birds  \n",
       "17                0         1          1     mammals  \n",
       "18                0         1          0     mammals  \n",
       "19                1         1          0       birds  \n",
       "20                0         1          0    reptiles  \n",
       "21                0         1          0     mammals  \n",
       "22                1         1          0       birds  \n",
       "23                0         1          0     mammals  \n",
       "24                0         0          0      fishes  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.tree import plot_tree\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC, NuSVC\n",
    "from sklearn.ensemble import BaggingClassifier, AdaBoostClassifier, RandomForestClassifier\n",
    "\n",
    "# train_dataset\n",
    "train_csv_file_path = 'Appendix1_vertebrate.csv'\n",
    "df_train = pd.read_csv(train_csv_file_path)\n",
    "\n",
    "# 显示原始数据表格\n",
    "print(\"原始数据：\")\n",
    "display(df_train)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "254bfd2b",
   "metadata": {},
   "source": [
    "显示修改后的数据(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2a31a441",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "修改后的数据：\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Warm-blooded</th>\n",
       "      <th>Gives Birth</th>\n",
       "      <th>Aquatic Creature</th>\n",
       "      <th>Aerial Creature</th>\n",
       "      <th>Has Legs</th>\n",
       "      <th>Hiberates</th>\n",
       "      <th>Class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>human</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>python</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>salmon</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>whale</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>frog</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>komodo</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>bat</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>pigeon</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>cat</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>leopard shark</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>turtle</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>penguin</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>porcupine</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>eel</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>salamander</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>koala</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>flamingo</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>beaver</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>giraffe</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>seagull</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>iguana</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>otter</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>albatross</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>kangaroo</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>trout</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Name  Warm-blooded  Gives Birth  Aquatic Creature  \\\n",
       "0           human             1            1                 0   \n",
       "1          python             0            0                 0   \n",
       "2          salmon             0            0                 1   \n",
       "3           whale             1            1                 1   \n",
       "4            frog             0            0                 1   \n",
       "5          komodo             0            0                 0   \n",
       "6             bat             1            1                 0   \n",
       "7          pigeon             1            0                 0   \n",
       "8             cat             1            1                 0   \n",
       "9   leopard shark             0            1                 1   \n",
       "10         turtle             0            0                 1   \n",
       "11        penguin             1            0                 1   \n",
       "12      porcupine             1            1                 0   \n",
       "13            eel             0            0                 1   \n",
       "14     salamander             0            0                 1   \n",
       "15          koala             1            1                 0   \n",
       "16       flamingo             1            0                 0   \n",
       "17         beaver             1            1                 0   \n",
       "18        giraffe             1            1                 0   \n",
       "19        seagull             1            0                 1   \n",
       "20         iguana             0            0                 0   \n",
       "21          otter             1            1                 0   \n",
       "22      albatross             1            0                 1   \n",
       "23       kangaroo             1            1                 0   \n",
       "24          trout             0            0                 1   \n",
       "\n",
       "    Aerial Creature  Has Legs  Hiberates  Class  \n",
       "0                 0         1          0      0  \n",
       "1                 0         0          1      1  \n",
       "2                 0         0          0      0  \n",
       "3                 0         0          0      0  \n",
       "4                 0         1          1      0  \n",
       "5                 0         1          0      1  \n",
       "6                 1         1          1      0  \n",
       "7                 1         1          0      0  \n",
       "8                 0         1          0      0  \n",
       "9                 0         0          0      0  \n",
       "10                0         1          0      1  \n",
       "11                0         1          0      0  \n",
       "12                0         1          1      0  \n",
       "13                0         0          0      0  \n",
       "14                0         1          1      0  \n",
       "15                0         1          0      0  \n",
       "16                1         1          0      0  \n",
       "17                0         1          1      0  \n",
       "18                0         1          0      0  \n",
       "19                1         1          0      0  \n",
       "20                0         1          0      1  \n",
       "21                0         1          0      0  \n",
       "22                1         1          0      0  \n",
       "23                0         1          0      0  \n",
       "24                0         0          0      0  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X_y_modified = df_train.copy()\n",
    "X_y_modified['Class'] = X_y_modified['Class'].apply(lambda x: 1 if x == 'reptiles' else 0)\n",
    "\n",
    "print(\"\\n修改后的数据：\")\n",
    "display(X_y_modified)\n",
    "\n",
    "X_train = X_y_modified.drop(['Name', 'Class'], axis=1)\n",
    "y_train = X_y_modified['Class']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc06d48b",
   "metadata": {},
   "source": [
    "正确训练分类器模型(5%)\n",
    "打印训练精度(5%)验证精度(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "91b022cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Decision Tree: Train Accuracy: 0.960, Test Accuracy: 1.000\n",
      "K-Nearest Neighbors: Train Accuracy: 0.840, Test Accuracy: 0.909\n",
      "Logistic Regression: Train Accuracy: 0.840, Test Accuracy: 0.727\n",
      "Linear SVM: Train Accuracy: 0.960, Test Accuracy: 1.000\n",
      "Non-linear SVM (RBF): Train Accuracy: 0.960, Test Accuracy: 0.909\n",
      "Bagging Classifier: Train Accuracy: 1.000, Test Accuracy: 1.000\n",
      "AdaBoost Classifier: Train Accuracy: 1.000, Test Accuracy: 0.727\n",
      "Random Forest: Train Accuracy: 0.840, Test Accuracy: 0.727\n",
      "Optimal Classifier: Decision Tree\n",
      "Train Accuracy: 0.960, Test Accuracy: 1.000\n",
      " \n"
     ]
    }
   ],
   "source": [
    "# test_dataset\n",
    "test_csv_file_path = 'Appendix3_testdata.csv'\n",
    "df_test = pd.read_csv(test_csv_file_path)\n",
    "X_test = df_test.drop(['Name', 'Class'], axis=1)\n",
    "y_test = df_test['Class'].apply(lambda x: 1 if x == 'reptiles' else 0)\n",
    "\n",
    "\n",
    "# 选择分类器模型\n",
    "models = {\n",
    "    'Decision Tree': DecisionTreeClassifier(max_depth=2),\n",
    "    'K-Nearest Neighbors': KNeighborsClassifier(),\n",
    "    'Logistic Regression': LogisticRegression(),\n",
    "    'Linear SVM': SVC(kernel='linear'),\n",
    "    'Non-linear SVM (RBF)': SVC(kernel='rbf'),\n",
    "    'Bagging Classifier': BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=10),\n",
    "    'AdaBoost Classifier': AdaBoostClassifier(DecisionTreeClassifier(max_depth=2)),\n",
    "    'Random Forest': RandomForestClassifier(n_estimators=10, max_depth=2)\n",
    "}\n",
    "\n",
    "# 训练与评估\n",
    "test_max_acc = 0\n",
    "optimal_classifier = 0\n",
    "train_acc_opt = 0\n",
    "for name, model in models.items():\n",
    "    model.fit(X_train, y_train)\n",
    "    train_acc = accuracy_score(y_train, model.predict(X_train))\n",
    "    test_acc = accuracy_score(y_test, model.predict(X_test))\n",
    "    if test_acc > test_max_acc:  # 按测试集准确率选取最佳分类器\n",
    "        test_max_acc = test_acc\n",
    "        optimal_classifier = name\n",
    "        train_acc_opt = train_acc\n",
    "    print(f\"{name}: Train Accuracy: {train_acc:.3f}, Test Accuracy: {test_acc:.3f}\")   \n",
    "print(f\"Optimal Classifier: {optimal_classifier}\\nTrain Accuracy: {train_acc_opt:.3f}, Test Accuracy: {test_max_acc:.3f}\\n \")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef4099e4",
   "metadata": {},
   "source": [
    "绘制分类器(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f7e2a373",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "\n",
    "matplotlib.use('TkAgg')\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "classifier = models[optimal_classifier]\n",
    "\n",
    "classifier.fit(X_train, y_train)\n",
    "\n",
    "plt.figure(figsize=(10, 8))\n",
    "_ = plot_tree(classifier, filled=True, feature_names=X_train.columns, class_names=['not-a-reptile', 'reptile'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fefefbe5",
   "metadata": {},
   "source": [
    "# 2\n",
    "正确训练分类器模型(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e9e7bfea",
   "metadata": {},
   "outputs": [],
   "source": [
    "classifier_unlimited_depth = DecisionTreeClassifier()\n",
    "classifier_unlimited_depth.fit(X_train, y_train)\n",
    "\n",
    "# 计算训练和测试准确率\n",
    "unrestricted_train_predictions = classifier_unlimited_depth.predict(X_train)\n",
    "unrestricted_test_predictions = classifier_unlimited_depth.predict(X_test)\n",
    "\n",
    "train_accuracy_unlimited_depth = accuracy_score(y_train, unrestricted_train_predictions)\n",
    "test_accuracy_unlimited_depth = accuracy_score(y_test, unrestricted_test_predictions)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "619e8f9d",
   "metadata": {},
   "source": [
    "绘制分类器(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ac747191",
   "metadata": {},
   "outputs": [],
   "source": [
    "classifier = DecisionTreeClassifier()\n",
    "\n",
    "classifier.fit(X_train, y_train)\n",
    "\n",
    "plt.figure(figsize=(10, 8))\n",
    "_ = plot_tree(classifier, filled=True, feature_names=X_train.columns, class_names=['not-a-reptile', 'reptile'])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dcd8957",
   "metadata": {},
   "source": [
    "给出结果(5%)\n",
    "对发现的解释或假设(5%)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b6bffe42",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train Accuracy (Unrestricted): 1.000\n",
      "Test Accuracy (Unrestricted): 1.000\n"
     ]
    }
   ],
   "source": [
    "print(f'Train Accuracy (Unrestricted): {train_accuracy_unlimited_depth:.3f}')\n",
    "print(f'Test Accuracy (Unrestricted): {test_accuracy_unlimited_depth:.3f}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ae55108",
   "metadata": {},
   "source": [
    "解释或假设\n",
    "1：因为没限制深度，模型过拟合，所以模型精度1.0。 \n",
    "2：深度2不是模型的最佳深度，所以在不限制深度之后，模型得到了更充分的训练，所以模型很快就趋近完美，没有过拟合。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6c1654b",
   "metadata": {},
   "source": [
    "# 3\n",
    "加载并显示数据(5%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9962bf1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始数据：\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Warm-blooded</th>\n",
       "      <th>Gives Birth</th>\n",
       "      <th>Aquatic Creature</th>\n",
       "      <th>Aerial Creature</th>\n",
       "      <th>Has Legs</th>\n",
       "      <th>Hiberates</th>\n",
       "      <th>Class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>human</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>python</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>salmon</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>whale</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>frog</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>amphibians</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>komodo</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>bat</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>pigeon</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>cat</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>leopard shark</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>turtle</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>penguin</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>porcupine</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>eel</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>salamander</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>amphibians</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>koala</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>flamingo</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>beaver</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>giraffe</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>seagull</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>iguana</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>reptiles</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>otter</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>albatross</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>birds</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>kangaroo</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>mammals</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>trout</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>fishes</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Name  Warm-blooded  Gives Birth  Aquatic Creature  \\\n",
       "0           human             1            1                 0   \n",
       "1          python             0            0                 0   \n",
       "2          salmon             0            0                 1   \n",
       "3           whale             1            1                 1   \n",
       "4            frog             0            0                 1   \n",
       "5          komodo             0            0                 0   \n",
       "6             bat             1            1                 0   \n",
       "7          pigeon             1            0                 0   \n",
       "8             cat             1            1                 0   \n",
       "9   leopard shark             0            1                 1   \n",
       "10         turtle             0            0                 1   \n",
       "11        penguin             1            0                 1   \n",
       "12      porcupine             1            1                 0   \n",
       "13            eel             0            0                 1   \n",
       "14     salamander             0            0                 1   \n",
       "15          koala             1            1                 0   \n",
       "16       flamingo             1            0                 0   \n",
       "17         beaver             1            1                 0   \n",
       "18        giraffe             1            1                 0   \n",
       "19        seagull             1            0                 1   \n",
       "20         iguana             0            0                 0   \n",
       "21          otter             1            1                 0   \n",
       "22      albatross             1            0                 1   \n",
       "23       kangaroo             1            1                 0   \n",
       "24          trout             0            0                 1   \n",
       "\n",
       "    Aerial Creature  Has Legs  Hiberates       Class  \n",
       "0                 0         1          0     mammals  \n",
       "1                 0         0          1    reptiles  \n",
       "2                 0         0          0      fishes  \n",
       "3                 0         0          0     mammals  \n",
       "4                 0         1          1  amphibians  \n",
       "5                 0         1          0    reptiles  \n",
       "6                 1         1          1     mammals  \n",
       "7                 1         1          0       birds  \n",
       "8                 0         1          0     mammals  \n",
       "9                 0         0          0      fishes  \n",
       "10                0         1          0    reptiles  \n",
       "11                0         1          0       birds  \n",
       "12                0         1          1     mammals  \n",
       "13                0         0          0      fishes  \n",
       "14                0         1          1  amphibians  \n",
       "15                0         1          0     mammals  \n",
       "16                1         1          0       birds  \n",
       "17                0         1          1     mammals  \n",
       "18                0         1          0     mammals  \n",
       "19                1         1          0       birds  \n",
       "20                0         1          0    reptiles  \n",
       "21                0         1          0     mammals  \n",
       "22                1         1          0       birds  \n",
       "23                0         1          0     mammals  \n",
       "24                0         0          0      fishes  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "csv_file_path = 'Appendix1_vertebrate.csv' \n",
    "df = pd.read_csv(csv_file_path)\n",
    "\n",
    "# 显示原始数据表格\n",
    "print(\"原始数据：\")\n",
    "display(df_train)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "762f4135",
   "metadata": {},
   "source": [
    "正确分割数据集(10%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8acef690",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop(['Name', 'Class'], axis=1)\n",
    "y = df['Class'].apply(lambda x: 1 if x == 'reptiles' else 0)\n",
    "X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.5, random_state=1)\n",
    "X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3abafbbc",
   "metadata": {},
   "source": [
    "找到最佳深度(10%)\n",
    "打印测试精度(10%)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a834bc56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Best Depth: 1\n",
      "Test Accuracy with Best Depth: 0.857\n"
     ]
    }
   ],
   "source": [
    "\n",
    "best_depth = None\n",
    "best_accuracy = 0\n",
    "\n",
    "for depth in range(1, 11):\n",
    "    classifier = DecisionTreeClassifier(max_depth=depth)\n",
    "\n",
    "    classifier.fit(X_train, y_train)\n",
    "\n",
    "    val_predictions = classifier.predict(X_val)\n",
    "\n",
    "    val_accuracy = accuracy_score(y_val, val_predictions)\n",
    "\n",
    "    if val_accuracy > best_accuracy:\n",
    "        best_accuracy = val_accuracy\n",
    "        best_depth = depth\n",
    "\n",
    "final_classifier = DecisionTreeClassifier(max_depth=best_depth)\n",
    "final_classifier.fit(pd.concat([X_train, X_val]), pd.concat([y_train, y_val]))\n",
    "\n",
    "test_predictions = final_classifier.predict(X_test)\n",
    "\n",
    "test_accuracy = accuracy_score(y_test, test_predictions)\n",
    "print(f'Best Depth: {best_depth}')\n",
    "print(f'Test Accuracy with Best Depth: {test_accuracy:.3f}')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42a5f843",
   "metadata": {},
   "source": [
    "为什么选择它(15%)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f46c43e",
   "metadata": {},
   "source": [
    "Because compared with other depths, if we use this depth, the accuracy will be the highest."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
